﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ChessSuite.Utilities.Common.Enumeations;

namespace ChessSuite.Utilities.Common.Utilities
{
    public static partial class PieceTypeUtility
    {
        public static PieceType Flatten(IEnumerable<PieceType> piece)
        {
            var pawns = piece.Count(PieceType.Pawn);
            var bishops = piece.Count(PieceType.Bishop);
            var knights = piece.Count(PieceType.Knight);
            var rooks = piece.Count(PieceType.Rook);
            var queens = piece.Count(PieceType.Queen);
            var kings = piece.Count(PieceType.King);

            //Debug.Assert(pawns < 2 && bishops < 2 && knights < 2 &&
            //    rooks < 2 && queens < 2 && kings < 2);
            if (pawns > 1 || bishops > 1 || knights > 1 ||
                rooks > 1 || queens > 1 || kings > 1)
                throw new ArgumentException("Unable to flatten a composite piece, since there are more pieces of the same type",
                    "piece");

            var res = PieceType.None;
            if (piece.Count(PieceType.Pawn) > 0) res |= PieceType.Pawn;
            if (piece.Count(PieceType.Bishop) > 0) res |= PieceType.Bishop;
            if (piece.Count(PieceType.Knight) > 0) res |= PieceType.Knight;
            if (piece.Count(PieceType.Rook) > 0) res |= PieceType.Rook;
            if (piece.Count(PieceType.Queen) > 0) res |= PieceType.Queen;
            if (piece.Count(PieceType.King) > 0) res |= PieceType.King;
            return res;
        }
    }
}
